'use strict';

const BaseController = require('./base');

class MiniProgramUsersController extends BaseController {
  constructor(ctx) {
    super(ctx, 'miniProgramUsers');
  }

  // 小程序登录
  async login() {
    const { ctx } = this;
    const { code } = ctx.request.body;
    
    if (!code) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.login(code);
    ctx.success(result);
  }

  // 绑定学生信息
  async bindStudent() {
    const { ctx } = this;
    const { openid, school_id, student_id, name } = ctx.request.body;
    
    if (!openid || !school_id || !student_id || !name) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.bindStudent(openid, school_id, student_id, name);
    ctx.success(result);
  }

  // 获取用户信息
  async getUserInfo() {
    const { ctx } = this;
    const { openid } = ctx.query;
    
    if (!openid) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.getUserInfo(openid);
    ctx.success(result);
  }

  // 获取轮播图
  async getCarousels() {
    const { ctx } = this;
    const result = await ctx.service.miniProgramUsers.getCarousels();
    ctx.success(result);
  }

  // 获取资讯列表
  async getNews() {
    const { ctx } = this;
    const { page = 1, size = 10 } = ctx.query;
    const result = await ctx.service.miniProgramUsers.getNews(parseInt(page), parseInt(size));
    ctx.success(result);
  }

  // 获取资讯详情
  async getNewsDetail() {
    const { ctx } = this;
    const { id } = ctx.query;
    
    if (!id) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.getNewsDetail(id);
    ctx.success(result);
  }

  // 获取学校列表
  async getSchools() {
    const { ctx } = this;
    const result = await ctx.service.miniProgramUsers.getSchools();
    ctx.success(result);
  }

  // 获取图书馆列表
  async getLibraries() {
    const { ctx } = this;
    const { school_id } = ctx.query;
    
    if (!school_id) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.getLibraries(school_id);
    ctx.success(result);
  }

  // 获取图书馆详情
  async getLibraryDetail() {
    const { ctx } = this;
    const { id } = ctx.query;
    
    if (!id) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.getLibraryDetail(id);
    ctx.success(result);
  }

  // 获取楼层列表
  async getFloors() {
    const { ctx } = this;
    const { library_id } = ctx.query;
    
    if (!library_id) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.getFloors(library_id);
    ctx.success(result);
  }

  // 获取区域列表
  async getAreas() {
    const { ctx } = this;
    const { floor_id } = ctx.query;
    
    if (!floor_id) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.getAreas(floor_id);
    ctx.success(result);
  }

  // 获取座位列表
  async getSeats() {
    const { ctx } = this;
    const { area_id, date } = ctx.query;
    
    if (!area_id || !date) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.getSeats(area_id, date);
    ctx.success(result);
  }

  // 预约座位
  async reserve() {
    const { ctx } = this;
    const { openid, library_id, seat_id, date, start_time, end_time } = ctx.request.body;
    
    if (!openid || !library_id || !seat_id || !date || !start_time || !end_time) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.reserve(openid, library_id, seat_id, date, start_time, end_time);
    ctx.success(result);
  }

  // 获取预约记录
  async getReservations() {
    const { ctx } = this;
    const { openid, status } = ctx.query;
    
    if (!openid) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.getReservations(openid, status);
    ctx.success(result);
  }

  // 签到
  async checkIn() {
    const { ctx } = this;
    const { id, openid } = ctx.request.body;
    
    if (!id || !openid) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.checkIn(id, openid);
    ctx.success(result);
  }

  // 签退
  async checkOut() {
    const { ctx } = this;
    const { id, openid } = ctx.request.body;
    
    if (!id || !openid) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.checkOut(id, openid);
    ctx.success(result);
  }

  // 取消预约
  async cancelReservation() {
    const { ctx } = this;
    const { id, openid } = ctx.request.body;
    
    if (!id || !openid) {
      ctx.throw(400, '缺少必要参数');
    }
    
    const result = await ctx.service.miniProgramUsers.cancelReservation(id, openid);
    ctx.success(result);
  }
}

module.exports = MiniProgramUsersController;
